package com.meihua.shellapp.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.AuthenticationInfoActivity;
import com.meihua.shellapp.activity.MainActivity;
import com.meihua.shellapp.activity.RegisterActivity;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.LoginBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import butterknife.OnClick;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_input_username, et_pwd_userpwd;

    private Button login_submit;//登录
    private long exitTime;//记录返回键按下时间

    @Override
    public int getLayoutId() {

        return R.layout.activity_login;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        et_input_username = (EditText) findViewById(R.id.et_input_username);
        et_pwd_userpwd = (EditText) findViewById(R.id.et_pwd_userpwd);
        login_submit = (Button) findViewById(R.id.login_submit);
        et_input_username.setText(SharedPreferencesUtil.getInstance().getString(mContext, "phone", ""));
    }

    @OnClick({R.id.forget_pwd, R.id.register, R.id.login_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.forget_pwd://忘记密码
                startActivity(new Intent(this, AuthenticationInfoActivity.class).putExtra("type", 3));
                break;
            case R.id.register://暂时不要，app端不要注册
                startActivity(new Intent(this, WebActivity.class).putExtra("url", URL.getUrl().REGISTER_URL));
                break;
            case R.id.login_submit://登录
                hiddenInputMethod();
                if (et_input_username.getText().toString().trim().equals("")) {
                    ToastUtils.getInstance().showToast("请输入手机号");
                } else if (et_pwd_userpwd.getText().toString().trim().equals("")) {
                    ToastUtils.getInstance().showToast("请输入密码");
                }
                login(et_input_username.getText().toString().trim(),
                        et_pwd_userpwd.getText().toString().trim());
                break;
        }
    }


    public static boolean isEmpty(String str) {
        return str == null || "".equals(str.trim()) || "null".equals(str);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    /**
     * 登录
     */
    private void login(final String phone, final String pwd) {
        showLoadingDialog();
        OkGo.<String>post(URL.getUrl().LOGIN)
                .params("userName", phone)
                .params("passWord", pwd)
                .execute(new MyStringCallBack<LoginBean>(login_submit) {
                    @Override
                    public void onSuccessData(LoginBean loginBean) {
                        super.onSuccessData(loginBean);
                        dismissLoadingDialog();
                        if (loginBean.getCode() == 1) {
                            SharedPreferencesUtil.getInstance().setObject(mContext, "user", loginBean.getData());
                            SharedPreferencesUtil.getInstance().saveString(mContext, "phone", phone);
                            SharedPreferencesUtil.getInstance().saveString(mContext, "passWord", pwd);
                            SharedPreferencesUtil.getInstance().saveString(mContext, Constant.PAY_PASSWARD, loginBean.getData().getIspay_pwd());
                            SharedPreferencesUtil.getInstance().saveBoolean(mContext, Constant.ISLOGIN,true);
                            MyApplication.getInstance().bean = loginBean.getData();
                            MyApplication.getInstance().bean.setPhone(phone);
                           // startActivity(new Intent(mContext, MainActivity.class));
                            finish();
                        } else {
                            ToastUtils.getInstance().showToast(loginBean.getMsg());
                        }

                    }
                });

//
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//        if (exitTime == 0 || System.currentTimeMillis() - exitTime > 2000) {
//            exitTime = System.currentTimeMillis();
//            ToastUtils.getInstance().showToast("再按一次退出");
//            return false;
//        } else {
//            MyApplication.getInstance().edit();
//            return false;
//        }
//    } else {
//        return super.onKeyDown(keyCode, event);
//    }
//}
}
